#! /bin/bash

MODE="$1"

edit () {
	MINUTE=`echo "$TREE" | cut -d" " -f1`
	HOUR=`echo "$TREE" | cut -d" " -f2`
	DAY=`echo "$TREE" | cut -d" " -f3`
	MONTH=`echo "$TREE" | cut -d" " -f4`
	WEEKDAY=`echo "$TREE" | cut -d" " -f5`
	TASK=`echo "$TREE" | cut -d" " -f6- | cut -d'#' -f1`
	TASKNAME=`echo "$TREE" | cut -d" " -f6- | cut -d'#' -f2-`
	OLD="$MINUTE $HOUR $DAY $MONTH $WEEKDAY $TASK""#$TASKNAME"

	if [ "$MINUTE" = "*" ]; then MINUTE="$(gettext 'any')"; else MINUTE=`printf "%02d" $MINUTE`; fi
	if [ "$HOUR" = "*" ]; then HOUR="$(gettext 'any')"; else HOUR=`printf "%02d" $HOUR`; fi
	if [ "$DAY" = "*" ]; then DAY="$(gettext 'any')"; else DAY="$DAY."; fi
	case $WEEKDAY in
		1)		WEEKDAY="$(gettext 'Mondays')";;
		2)		WEEKDAY="$(gettext 'Tuesdays')";;
		3)		WEEKDAY="$(gettext 'Wednesdays')";;
		4)		WEEKDAY="$(gettext 'Thursdays')";;
		5)		WEEKDAY="$(gettext 'Fridays')";;
		6)		WEEKDAY="$(gettext 'Saturdays')";;
		0)		WEEKDAY="$(gettext 'Sundays')";;
		"*")	WEEKDAY="$(gettext 'any')";;
	esac
	case "$MONTH" in
		1)		MONTH="$(gettext 'January')";;
		2)		MONTH="$(gettext 'February')";;
		3)		MONTH="$(gettext 'March')";;
		4)		MONTH="$(gettext 'April')";;
		5)		MONTH="$(gettext 'May')";;
		6)		MONTH="$(gettext 'June')";;
		7)		MONTH="$(gettext 'July')";;
		8)		MONTH="$(gettext 'August')";;
		9)		MONTH="$(gettext 'September')";;
		10)		MONTH="$(gettext 'October')";;
		11)		MONTH="$(gettext 'November')";;
		12)		MONTH="$(gettext 'December')";;
		"*")	MONTH="$(gettext 'any')";;
	esac
	ITEMS_HOUR="<item>$HOUR</item>$ITEMS_HOUR"
	ITEMS_MINUTE="<item>$MINUTE</item>$ITEMS_MINUTE"
	ITEMS_DAY="<item>$DAY</item>$ITEMS_DAY"	
	ITEMS_MONTH="<item>$MONTH</item>$ITEMS_MONTH"	
	ITEMS_WEEKDAY="<item>$WEEKDAY</item>$ITEMS_WEEKDAY"	
}

new () {
	if [ "$MODE" != "-edit" ] && [ ! "$TASK" ]; then TASK='gxmessage "Happy Puppy"'; TASKNAME=''; fi

	#gtk-theme
	echo 'style "bar" { font_name="bold italic" } widget_class "*.GtkStatusbar*" style "bar"' > /tmp/pschedule-gtkrc
	export GTK2_RC_FILES=/tmp/pschedule-gtkrc:/root/.gtkrc-2.0 #include theme stuff with system themes

	S='
	<window title="pSchedule - '$(gettext 'Add task')'" icon-name="pschedule">
	 <vbox space-expand="true" space-fill="true">
	  <hbox space-expand="false" space-fill="false">
	   <vbox space-expand="true" space-fill="true">
	    <hbox space-expand="true" space-fill="true">
	     <text space-expand="false" space-fill="false"><label>'$(gettext 'Command')':</label></text>
	      <entry space-expand="true" space-fill="true"><variable>TASK</variable><input>echo "'$TASK'"</input></entry>
	    </hbox>
	    <hbox space-expand="true" space-fill="true">
	     <text space-expand="false" space-fill="false"><label>'$(gettext 'Task name (optional)')':</label></text>
	      <entry space-expand="true" space-fill="true"><variable>TASKNAME</variable><input>echo "'$TASKNAME'"</input></entry>
	    </hbox>
	   </vbox>
	   <hbox space-expand="false" space-fill="false">'
	    [ "$SILENT" = "true" ] && S=$S'
	    <button label="  '$(gettext 'Show all tasks')'  " space-expand="false" space-fill="false">
	     <action>TASK=""; pschedule &</action>
	     <action>exit:EXIT</action>
	    </button>'
	    S=$S'<button space-expand="false" space-fill="false" image-position="2" tooltip-text="'$(gettext 'Add these settings back to task list')'">'
	     [ -f /usr/share/pixmaps/puppy/clock_interval.svg ] && S=$S'<input file>/usr/share/pixmaps/puppy/clock_interval.svg</input>' || S=$S'<input file stock="gtk-add"></input>'
	     S=$S'<height>48</height>
         <action type="exit">add</action>
	    </button>
	   </hbox>
	  </hbox>
	  <vbox space-expand="true" space-fill="true">
	   <frame '$(gettext 'Time')'>
	   <hbox space-expand="true" space-fill="true">
	    <table hscrollbar-policy="2" space-expand="true" space-fill="true">
	     <width>80</width><height>200</height>
		 <label>'$(gettext 'Hour')'</label>
		 <variable>HOUR</variable>
		 '$ITEMS_HOUR'
		 <action>. '$PROGPATH'/func_human_to_cron -a</action>
		 <action>. '$PROGPATH'/func_cron_to_human -new -a</action>
		 <action>refresh:ENTRY_NEXT</action>
	    </table>
	    <table hscrollbar-policy="2" space-expand="true" space-fill="true">
		 <width>80</width><height>100</height>
		 <label>'$(gettext 'Minute')'</label>
		 <variable>MINUTE</variable>
		 '$ITEMS_MINUTE'
		 <action>. '$PROGPATH'/func_human_to_cron -a</action>
		 <action>. '$PROGPATH'/func_cron_to_human -new -a</action>
		 <action>refresh:ENTRY_NEXT</action>
	    </table>
	    <table hscrollbar-policy="2" space-expand="true" space-fill="true">
	 	 <width>140</width><height>100</height>
		 <label>'$(gettext 'Day')'</label>
		 <variable>WEEKDAY</variable>
		 '$ITEMS_WEEKDAY'
		 <action>. '$PROGPATH'/func_human_to_cron -a</action>
		 <action>. '$PROGPATH'/func_cron_to_human -new -a</action>
		 <action>refresh:ENTRY_NEXT</action>
	    </table>
	    <table hscrollbar-policy="2" space-expand="true" space-fill="true">
		 <width>80</width><height>100</height>
 		 <label>'$(gettext 'Date')'</label>
		 <variable>DAY</variable>
		 '$ITEMS_DAY'
		 <action>. '$PROGPATH'/func_human_to_cron -a</action>
		 <action>. '$PROGPATH'/func_cron_to_human -new -a</action>
		 <action>refresh:ENTRY_NEXT</action>
	    </table>
	    <table hscrollbar-policy="2" space-expand="true" space-fill="true">
	     <width>140</width><height>100</height>
	     <label>'$(gettext 'Month')'</label>
	     <variable>MONTH</variable>
	     '$ITEMS_MONTH'
	     <action>. '$PROGPATH'/func_human_to_cron -a</action>
	     <action>. '$PROGPATH'/func_cron_to_human -new</action>
	     <action>refresh:ENTRY_NEXT</action>
	    </table>
	   </hbox>
	  </frame>
	 </vbox>
	 <statusbar space-expand="false" space-fill="false"><variable>ENTRY_NEXT</variable><input file>/tmp/pschedule-next</input></statusbar>
	</vbox>
	<action signal="show">rm -f /tmp/pschedule-loading</action>
	</window>'
	export new_task="$S"
	I=$IFS; IFS=""
	for STATEMENTS in  $($GTKDIALOG -p new_task); do
		eval $STATEMENTS
	done
	IFS=$I
	TASK="`echo "$TASK #$TASKNAME" | sed 's%&%\\\&%g'`" #& corrupts tasklist in main window
	case $EXIT in
		add)
			. $PROGPATH/func_human_to_cron -a
			. $PROGPATH/func_cron_to_human -new
			CRON_COMMAND="$(</tmp/pschedule-cron)"
			if [ "$MODE" = "-edit" ]; then
				sed -i -e 's%*%{ð®STARđþ}%g' /var/spool/cron/crontabs/$USER #150401
				OLD=`echo "$OLD" | sed -e 's%*%{ð®STARđþ}%g'`
				PATTERN="s%$OLD%$CRON_COMMAND $TASK%g"
				sed -i -e "$PATTERN" /var/spool/cron/crontabs/$USER #150401
				sed -i -e 's%{ð®STARđþ}%*%g' /var/spool/cron/crontabs/$USER #150401
			else
				echo "$CRON_COMMAND $TASK" >> /var/spool/cron/crontabs/$USER #150401
			fi
			[ -x /etc/init.d/crond ] && /etc/init.d/crond restart
			;;
		*)	exit;;
	esac
}

if [ "$MODE" = "-edit" ]; then
	TREE=`cat /tmp/pschedule-treeitems | grep "$TREE" | cut -d "|" -f 3 | cut -c 2-`
	[ "$TREE" ] && edit #generate lists of cron command
	[ ! "$TREE" ] && MODE='' #the -e switch will be overlooked. This is a new task
else
	echo "*"	> /tmp/pschedule-cronminute
	echo "*"	> /tmp/pschedule-cronhour
	echo "*"	> /tmp/pschedule-cronday
	echo "*"	> /tmp/pschedule-cronmonth
	echo "*"	> /tmp/pschedule-cronweekday
	. $PROGPATH/func_cron_to_human -new -a
fi

echo true > /tmp/pschedule-loading #to avoid many calls to human_to_cron / cron_to_human during startup of editor window
new
